package com.lagou.edu.annotation;


import java.sql.SQLException;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

import com.lagou.edu.utils.ConnectionUtils;


//@Aspect
//@Component
/**
 * 方式二
 * 切面控制事务
 *
 */
public class TransactionAspect {
	
	@Autowired
    private ConnectionUtils connectionUtils;
	
	

    @Pointcut("@annotation(com.lagou.edu.annotation.Transactional)")
    public void pointCut() {}
    
    
    @Around("pointCut()")
    public Object before(ProceedingJoinPoint pjp) throws Throwable {
    	connectionUtils.getCurrentThreadConn().setAutoCommit(false);
    	Object proceed =null;
    	try {
    		proceed = pjp.proceed();
    		connectionUtils.getCurrentThreadConn().commit();
		} catch (Throwable e) {
			connectionUtils.getCurrentThreadConn().rollback();
			throw e;
		}
    	return proceed;
    }
    
	
}
